﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CharacterCreation.Data.Services {
    /// <summary>
    /// All the services needed to roll dice and get scores.
    /// </summary>
    public static class DiceService {

        private static Random _random = new Random();

        /// <summary>
        /// Returns the result of a six sided dice roll.
        /// </summary>
        /// <returns></returns>
        public static int D6() {
            return _random.Next(1, 7);
        }

        /// <summary>
        /// Returns the sum of a number of D6's.
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static int D6(int number) {
            int result = 0;
            for (int i = 0; i < number; i++) {
                result += D6();
            }
            return result;
        }

        /// <summary>
        /// Returns teh result of a 100 sided dice roll.
        /// </summary>
        /// <returns></returns>
        public static int D100() {
            return _random.Next(1, 100);
        }

        /// <summary>
        /// Returns the result of a stat roll.
        /// </summary>
        /// <returns></returns>
        public static int StatRoll() {
            int result = D6(4);
            if (result > 20) {
                int exception = D6();
                if (exception == 6) exception += D6();

                result += exception;
            }
            return result;
        }

        /// <summary>
        /// return a collection of statistics.
        /// </summary>
        /// <returns></returns>
        public static int[] Stats() {
            int[] result = new int[10];
            int total = 0;

            while (total < 155) {
                total = 0;
                for (int i = 0; i < 10; i++) {
                    result[i] = StatRoll();
                }
                total = result.Sum();
            }

            return result;
        }
    }
}
